//using Firebase.Analytics;
using PlayInfinity.Laveda.Core.UI;
using System.Collections.Generic;
using Umeng;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MoPubEventListener : MonoBehaviour
{
	[SerializeField]
	private bool _rewardReceived;

	private void Awake()
	{
	}

	private void OnEnable()
	{
		MoPubManager.OnSdkInitializedEvent += OnSdkInitializedEvent;
		MoPubManager.OnConsentStatusChangedEvent += OnConsentStatusChangedEvent;
		MoPubManager.OnConsentDialogLoadedEvent += OnConsentDialogLoadedEvent;
		MoPubManager.OnConsentDialogFailedEvent += OnConsentDialogFailedEvent;
		MoPubManager.OnConsentDialogShownEvent += OnConsentDialogShownEvent;
		MoPubManager.OnAdLoadedEvent += OnAdLoadedEvent;
		MoPubManager.OnAdFailedEvent += OnAdFailedEvent;
		MoPubManager.OnAdClickedEvent += OnAdClickedEvent;
		MoPubManager.OnAdExpandedEvent += OnAdExpandedEvent;
		MoPubManager.OnAdCollapsedEvent += OnAdCollapsedEvent;
		MoPubManager.OnInterstitialLoadedEvent += OnInterstitialLoadedEvent;
		MoPubManager.OnInterstitialFailedEvent += OnInterstitialFailedEvent;
		MoPubManager.OnInterstitialShownEvent += OnInterstitialShownEvent;
		MoPubManager.OnInterstitialClickedEvent += OnInterstitialClickedEvent;
		MoPubManager.OnInterstitialDismissedEvent += OnInterstitialDismissedEvent;
		MoPubManager.OnInterstitialExpiredEvent += OnInterstitialExpiredEvent;
		MoPubManager.OnRewardedVideoLoadedEvent += OnRewardedVideoLoadedEvent;
		MoPubManager.OnRewardedVideoFailedEvent += OnRewardedVideoFailedEvent;
		MoPubManager.OnRewardedVideoExpiredEvent += OnRewardedVideoExpiredEvent;
		MoPubManager.OnRewardedVideoShownEvent += OnRewardedVideoShownEvent;
		MoPubManager.OnRewardedVideoClickedEvent += OnRewardedVideoClickedEvent;
		MoPubManager.OnRewardedVideoFailedToPlayEvent += OnRewardedVideoFailedToPlayEvent;
		MoPubManager.OnRewardedVideoReceivedRewardEvent += OnRewardedVideoReceivedRewardEvent;
		MoPubManager.OnRewardedVideoClosedEvent += OnRewardedVideoClosedEvent;
		MoPubManager.OnRewardedVideoLeavingApplicationEvent += OnRewardedVideoLeavingApplicationEvent;
	}

	private void OnDisable()
	{
		MoPubManager.OnSdkInitializedEvent -= OnSdkInitializedEvent;
		MoPubManager.OnConsentStatusChangedEvent -= OnConsentStatusChangedEvent;
		MoPubManager.OnConsentDialogLoadedEvent -= OnConsentDialogLoadedEvent;
		MoPubManager.OnConsentDialogFailedEvent -= OnConsentDialogFailedEvent;
		MoPubManager.OnConsentDialogShownEvent -= OnConsentDialogShownEvent;
		MoPubManager.OnAdLoadedEvent -= OnAdLoadedEvent;
		MoPubManager.OnAdFailedEvent -= OnAdFailedEvent;
		MoPubManager.OnAdClickedEvent -= OnAdClickedEvent;
		MoPubManager.OnAdExpandedEvent -= OnAdExpandedEvent;
		MoPubManager.OnAdCollapsedEvent -= OnAdCollapsedEvent;
		MoPubManager.OnInterstitialLoadedEvent -= OnInterstitialLoadedEvent;
		MoPubManager.OnInterstitialFailedEvent -= OnInterstitialFailedEvent;
		MoPubManager.OnInterstitialShownEvent -= OnInterstitialShownEvent;
		MoPubManager.OnInterstitialClickedEvent -= OnInterstitialClickedEvent;
		MoPubManager.OnInterstitialDismissedEvent -= OnInterstitialDismissedEvent;
		MoPubManager.OnInterstitialExpiredEvent -= OnInterstitialExpiredEvent;
		MoPubManager.OnRewardedVideoLoadedEvent -= OnRewardedVideoLoadedEvent;
		MoPubManager.OnRewardedVideoFailedEvent -= OnRewardedVideoFailedEvent;
		MoPubManager.OnRewardedVideoExpiredEvent -= OnRewardedVideoExpiredEvent;
		MoPubManager.OnRewardedVideoShownEvent -= OnRewardedVideoShownEvent;
		MoPubManager.OnRewardedVideoClickedEvent -= OnRewardedVideoClickedEvent;
		MoPubManager.OnRewardedVideoFailedToPlayEvent -= OnRewardedVideoFailedToPlayEvent;
		MoPubManager.OnRewardedVideoReceivedRewardEvent -= OnRewardedVideoReceivedRewardEvent;
		MoPubManager.OnRewardedVideoClosedEvent -= OnRewardedVideoClosedEvent;
		MoPubManager.OnRewardedVideoLeavingApplicationEvent -= OnRewardedVideoLeavingApplicationEvent;
	}

	private void AdFailed(string adUnitId, string action, string error)
	{
		string text = "Failed to " + action + " ad unit " + adUnitId;
		if (!string.IsNullOrEmpty(error))
		{
			text = text + ": " + error;
		}
		DebugUtils.LogError(DebugType.Other, text);
	}

	private void OnSdkInitializedEvent(string adUnitId)
	{
		DebugUtils.Log(DebugType.Other, "OnSdkInitializedEvent: " + adUnitId);
		// if (MoPubAndroid.ShouldShowConsentDialog)
		// {
		// 	MoPubAndroid.LoadConsentDialog();
		// }
		MopubManager.Instance.PreloadAds();
	}

	private void OnConsentStatusChangedEvent(MoPubBase.Consent.Status oldStatus, MoPubBase.Consent.Status newStatus, bool canCollectPersonalInfo)
	{
		DebugUtils.Log(DebugType.Other, "OnConsetStatusChangedEvent: old=" + oldStatus + " new=" + newStatus + " personalInfoOk=" + canCollectPersonalInfo.ToString());
	}

	private void OnConsentDialogLoadedEvent()
	{
		DebugUtils.Log(DebugType.Other, "OnConsentDialogLoadedEvent: dialog loaded");
		MoPubAndroid.ShowConsentDialog();
	}

	private void OnConsentDialogFailedEvent(string err)
	{
		DebugUtils.Log(DebugType.Other, "OnConsentDialogFailedEvent: " + err);
	}

	private void OnConsentDialogShownEvent()
	{
		DebugUtils.Log(DebugType.Other, "OnConsentDialogShownEvent: dialog shown");
	}

	private void OnAdLoadedEvent(string adUnitId, float height)
	{
		DebugUtils.Log(DebugType.Other, "OnAdLoadedEvent: " + adUnitId + " height: " + height);
		bool bannerActive = PlayInfinityAdManager.Instance.bannerActive;
		PlayInfinityAdManager.Instance.bannerActive = true;
	}

	private void OnAdFailedEvent(string adUnitId, string error)
	{
		AdFailed(adUnitId, "load banner", error);
	}

	private void OnAdClickedEvent(string adUnitId)
	{
		//FirebaseAnalytics.LogEvent("banner_tap", FirebaseAnalytics.ParameterValue, 1);
		DebugUtils.Log(DebugType.Other, "OnAdClickedEvent: " + adUnitId);
	}

	private void OnAdExpandedEvent(string adUnitId)
	{
		DebugUtils.Log(DebugType.Other, "OnAdExpandedEvent: " + adUnitId);
	}

	private void OnAdCollapsedEvent(string adUnitId)
	{
		DebugUtils.Log(DebugType.Other, "OnAdCollapsedEvent: " + adUnitId);
	}

	private void OnInterstitialLoadedEvent(string adUnitId)
	{
		DebugUtils.Log(DebugType.Other, "OnInterstitialLoadedEvent: " + adUnitId);
		MopubManager.Instance.interstitialLoaded = true;
	}

	private void OnInterstitialFailedEvent(string adUnitId, string error)
	{
		AdFailed(adUnitId, "load interstitial", error);
	}

	private void OnInterstitialShownEvent(string adUnitId)
	{
		DebugUtils.Log(DebugType.Other, "OnInterstitialShownEvent: " + adUnitId);
	}

	private void OnInterstitialClickedEvent(string adUnitId)
	{
		//FirebaseAnalytics.LogEvent("interstitial_tap", FirebaseAnalytics.ParameterValue, 1);
		DebugUtils.Log(DebugType.Other, "OnInterstitialClickedEvent: " + adUnitId);
	}

	private void OnInterstitialDismissedEvent(string adUnitId)
	{
		DebugUtils.Log(DebugType.Other, "OnInterstitialDismissedEvent: " + adUnitId);
		MopubManager.Instance.interstitialLoaded = false;
		MopubManager.Instance.RequestInterstitial();
	}

	private void OnInterstitialExpiredEvent(string adUnitId)
	{
		DebugUtils.Log(DebugType.Other, "OnInterstitialExpiredEvent: " + adUnitId);
	}

	private void OnRewardedVideoLoadedEvent(string adUnitId)
	{
		DebugUtils.Log(DebugType.Other, "OnRewardedVideoLoadedEvent: " + adUnitId);
		//MoPubAndroid.GetAvailableRewards(adUnitId);
	}

	private void OnRewardedVideoFailedEvent(string adUnitId, string error)
	{
		AdFailed(adUnitId, "load rewarded video", error);
	}

	private void OnRewardedVideoExpiredEvent(string adUnitId)
	{
		DebugUtils.Log(DebugType.Other, "OnRewardedVideoExpiredEvent: " + adUnitId);
	}

	private void OnRewardedVideoShownEvent(string adUnitId)
	{
		DebugUtils.Log(DebugType.Other, "OnRewardedVideoShownEvent: " + adUnitId);
	}

	private void OnRewardedVideoClickedEvent(string adUnitId)
	{
		//FirebaseAnalytics.LogEvent("rev_tap", FirebaseAnalytics.ParameterValue, 1);
		DebugUtils.Log(DebugType.Other, "OnRewardedVideoClickedEvent: " + adUnitId);
	}

	private void OnRewardedVideoFailedToPlayEvent(string adUnitId, string error)
	{
		AdFailed(adUnitId, "play rewarded video", error);
	}

	private void OnRewardedVideoReceivedRewardEvent(string adUnitId, string label, float amount)
	{
		DebugUtils.Log(DebugType.Other, "OnRewardedVideoReceivedRewardEvent for ad unit id " + adUnitId + " currency:" + label + " amount:" + amount);
		_rewardReceived = true;
	}

	private void OnRewardedVideoClosedEvent(string adUnitId)
	{
		DebugUtils.Log(DebugType.Other, "OnRewardedVideoClosedEvent: " + adUnitId);
		MopubManager.Instance.RequestRewardedVideo();
		if (_rewardReceived)
		{
			Analytics.Event("ShowVideo", new Dictionary<string, string>
			{
				{
					"ShowVideo",
					"ShowVideo_Success"
				}
			});
			DebugUtils.Log(DebugType.NetWork, "ShowVideo_Success");
			if (SceneManager.GetActiveScene().name == "CastleScene")
			{
				TaskPanelManager.Instance.WatchVideoFinish();
			}
			else
			{
				GameWinDlg.Instance.WatchVideoFinish();
			}
		}
		_rewardReceived = false;
	}

	private void OnRewardedVideoLeavingApplicationEvent(string adUnitId)
	{
		DebugUtils.Log(DebugType.Other, "OnRewardedVideoLeavingApplicationEvent: " + adUnitId);
	}
}
